import axios from "axios";
import NProgress from "./progress";
import { showFailToast, showToast } from "vant";
import { getToken, removeToken } from "./auth";
import router from "@/router";

// 默认 axios 实例请求配置
const defaultConfig = {
  headers: {
    "Content-Type": "application/json;charset=utf-8",
  },
  // 超时
  timeout: 50000,
  // axios中请求配置有baseURL选项，表示请求URL公共部分
  baseURL: import.meta.env.VITE_APP_BASE_API,
};

// 创建axios实例
const service = axios.create(defaultConfig);

// 请求拦截
service.interceptors.request.use(
  (config) => {
    NProgress.start();
    // 发送请求前，可在此携带 token
    const token = getToken();
    if (token) {
      config.headers["token"] = token;
      config.headers.Authorization = `Bearer ${token}`;
    }

    // FormData数据去请求头Content-Type
    if (config.data instanceof FormData) {
      delete config.headers["Content-Type"];
    }
    return config;
  },
  (error) => {
    showFailToast(error.message);
    return Promise.reject(error);
  }
);

// 响应拦截
service.interceptors.response.use(
  (res) => {
    NProgress.done();
    // 二进制数据则直接返回
    if (
      res.request.responseType === "blob" ||
      res.request.responseType === "arraybuffer"
    ) {
      return res.data;
    }
    // 与后端协定的返回字段
    let { code, msg, data } = res.data;
    // 未设置状态码则默认成功状态
    code = code || 200;
    if (code === 401) {
      // 未登录处理
      removeToken();
      showFailToast("登录状态已过期，请重新登录");
      router.push("/h5/login");
      return Promise.reject();
    } else {
      return Promise.resolve(res.data);
    }
  },
  (error) => {
    NProgress.done();
    // 处理 HTTP 网络错误
    // let prefix = `请求出错,地址: ${error?.config?.url}`;
    let { message, response } = error;
    if (response?.data.code === 401) {
      removeToken();
      showFailToast("登录状态已过期，请重新登录");
      router.push("/h5/login");
      return Promise.reject();
    }
    if (message == "Network Error") {
      message = "网络异常";
    } else if (message.includes("timeout")) {
      message = "系统接口请求超时";
    } else if (message.includes("Request failed with status code")) {
      message = "系统接口" + message.substr(message.length - 3) + "异常";
    }
    showFailToast(message);
    return Promise.reject(error);
  }
);

export default service;
